package com.jeesite.modules.digitalize.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.digitalize.dao.DigitalizeTaskArchiveDao;
import com.jeesite.modules.digitalize.entity.DigitalizeTask;
import com.jeesite.modules.digitalize.entity.DigitalizeTaskArchive;
import com.jeesite.modules.digitalize.entity.dto.DigitalizeTaskArchiveDTO;
import com.jeesite.modules.digitalize.entity.dto.DigitalizeTaskDelDTO;
import com.jeesite.modules.digitalize.util.minio.MinioFileUtil;
import com.jeesite.modules.digitalize.util.response.ResultVo;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 关联关系表(DigitalizeTaskArchive)表服务实现类
 *
 * @author mxz
 * @since 2023-11-15 16:23:44
 */
@Service("digitalizeTaskArchiveService")
public class DigitalizeTaskArchiveService extends ServiceImpl<DigitalizeTaskArchiveDao, DigitalizeTaskArchive> {

    @Resource
    private UserService userService;

    @Resource
    @Lazy
    private DigitalizeTaskService digitalizeTaskService;

    @Resource
    private MinioFileUtil minioFileUtil;

    public Page<DigitalizeTaskArchive> pageList(DigitalizeTaskArchiveDTO dto) {
        //查询任务
        LambdaQueryWrapper<DigitalizeTask> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DigitalizeTask::getDelStatus, 0);
        lambdaQueryWrapper.eq(DigitalizeTask::getProjectId, dto.getProjectId());
        List<DigitalizeTask> list = digitalizeTaskService.list(lambdaQueryWrapper);
        Page<DigitalizeTaskArchive> page = new Page(dto.getPageNo(), dto.getPageSize());
        LambdaQueryWrapper<DigitalizeTaskArchive> lq = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(dto.getFileName())) {
            lq.eq(DigitalizeTaskArchive::getFileName, dto.getFileName());
        }
        if (!CollectionUtil.isEmpty(list)) {
            lq.in(DigitalizeTaskArchive::getTaskId, list.stream().map(e -> e.getId()).collect(Collectors.toList()));
        }
        lq.eq(DigitalizeTaskArchive::getDelStatus, 0);
        page = this.baseMapper.selectPage(page, lq);
        if (CollectionUtil.isNotEmpty(page.getRecords())) {
            suppendUserName(page.getRecords());
        }
        return page;
    }


    private void suppendUserName(List<DigitalizeTaskArchive> list) {
        Map<String, User> userMap = new HashMap<>();
        list.forEach(chartVo -> {
            User user = userMap.computeIfAbsent(chartVo.getCreateBy(), userId -> userService.get(userId));
            if (user != null) {
                chartVo.setCreateBy(user.getUserName());
            }
        });
    }

    public ResultVo delBatch(DigitalizeTaskDelDTO dto) {
        if (CollectionUtil.isEmpty(dto.getIds())) {
            return ResultVo.success();
        }
        LambdaUpdateWrapper<DigitalizeTaskArchive> luw = new LambdaUpdateWrapper<>();
        luw.set(DigitalizeTaskArchive::getDelStatus,1);
        luw.in(DigitalizeTaskArchive::getId,dto.getIds());
        this.baseMapper.update(null,luw);
        return ResultVo.success();
    }

    public void downloadFile(HttpServletResponse response, Long id) {
        DigitalizeTaskArchive archive = this.baseMapper.selectById(id);
        minioFileUtil.downloadFile(response, archive.getFileUrl());
    }
}

